package hzau.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import hzau.constants.PageConstant;
import hzau.constants.ResultConstant;
import hzau.dao.CourseDao;
import hzau.pojo.Course;
import hzau.pojo.requestParams.CourseRegisterParam;
import hzau.result.Result;
import hzau.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    CourseDao courseDao;
    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public Result register(CourseRegisterParam course) {
        LambdaQueryWrapper<Course> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Course::getName,course.getName());
        lqw.eq(Course::getCollegeId,course.getCollegeId());
        Course course1=courseDao.selectOne(lqw);
        if (course1==null){
            courseDao.insert(new Course(course));
            return Result.success(null);
        }else
            return Result.error(ResultConstant.ALREADY_REGISTER_CODE,ResultConstant.COURSE_ALREADY_EXIST);
    }

    @Override
    public Result delete(Long id) {

        Course course = courseDao.selectById(id);
        if (course!=null){
            deleteFromRedis(id);
            courseDao.deleteById(id);
            return Result.success(null);
        }
        else return Result.error(ResultConstant.NOT_EXIST,ResultConstant.NO_SUCH_COURSE);
    }

    @Override
    public void deleteFromRedis(Long collegeId) {
        String key="college_course_"+collegeId.toString();
        redisTemplate.delete(key);
    }

    @Override
    public void updateRedis(Long collegeId) {
        String key="college_course_"+collegeId.toString();
        LambdaQueryWrapper<Course> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Course::getCollegeId,collegeId);
        List<Course> courseList = courseDao.selectList(lqw);
        redisTemplate.opsForValue().set(key,courseList);
    }

    @Override
    public Result update(Long courseId, CourseRegisterParam course) {
        deleteFromRedis(courseId);
        Course course1=new Course(course);
        course1.setId(courseId);
        courseDao.updateById(course1);
        return Result.success(null);
    }

    @Override
    public Result getAll(Long collegeId, Integer pageNum) {
        IPage<Course> page=new Page<>(pageNum,PageConstant.PAGE_SIZE);
        //先在Redis中找


            LambdaQueryWrapper<Course> lqw=new LambdaQueryWrapper<>();
            lqw.eq(Course::getCollegeId,collegeId);
            IPage<Course> courseList = courseDao.selectPage(page,lqw);
            if (courseList!=null){
            return Result.success(courseList);}
            else {
                return Result.error(ResultConstant.NOT_EXIST,ResultConstant.NO_SUCH_COURSE);
            }

    }

    @Override
    public Result getOne(Long courseId) {
        Course course = courseDao.selectById(courseId);
        if(course==null){
            return Result.error(ResultConstant.NOT_EXIST,ResultConstant.NO_SUCH_COURSE);

        }
        else return Result.success(course);
    }

    @Override
    public Result alterCreateTeacher(Long courseId, Long teacherId) {
        Course course = courseDao.selectById(courseId);
        course.setCreatePaperId(teacherId);
        courseDao.updateById(course);
        return Result.success(null);
    }

    @Override
    public Result alterReviewTeacher(Long courseId, Long teacherId) {
        Course course = courseDao.selectById(courseId);
        course.setReviewPaperId(teacherId);
        courseDao.updateById(course);
        return Result.success(null);
    }
}
